require.config({
    baseUrl: 'js/common'
});

require(['common'], function(defaultStr) {
    var baseUrl = defaultStr();
    /*----------
     *ajax数据请求
     *部门页面管理————补助页面
     *部门页面管理————补发页面
     *默认打开该页的时候就已经知道了是那个部门
     *并且只显示该部门的信息
     */

    var buzhu_table = new Vue({
        el: '#main-data',
        data: {
            items: [],
            /*是将显示在已添加列表中的数据*/
            new_item: [],
            query: {
                "rows": 10,
                "page": 1,
                // "deptId": "006"
            },
            add_item: '',
            temp: '',
            shenhe_items: [],
            dates: '',
            url: {
                'queryZG': baseUrl + '/base/stu/list.do',
                'addBuzhu': baseUrl + '/usr/buzhu/addBuZhuCheck.do',
                'addBuzhu_without': baseUrl + '/usr/buzhu/addBuZhuWithoutCheck.do',
                'submit': baseUrl + '/usr/buzhu/submit.do',
                'delBuzhu': baseUrl + '/usr/buzhu/remove.do',
                'getMonths': baseUrl + '/usr/date/getAll.do',
                'queryBuZhu': baseUrl + "/usr/buzhu/getBuzhuByPage.do",
            }
        },
        methods: {
            /*初始化页面*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryMonth();
                console.log(_self.dates);
            },
            queryMonth: function() {
                var _self = this;
                _self.dates = [];
                $.ajax({
                    type: "GET",
                    url: _self.url.getMonths,
                    dataType: "json",
                    success: function(result) {
                        console.log(result);
                        $.each(result.body, function(i, value) {
                            if (parseInt(value.helpMonth) > 10) {
                                var temp = value.helpYear + '' + value.helpMonth
                            } else {
                                var temp = value.helpYear + '0' + value.helpMonth

                            }
                            _self.dates.push(temp);
                        })
                    }
                })
            },
            /*添加数据到下方的表中，不审核*/
            join: function() {
                var _self = this;
                console.log(_self.temp.remark);
                if (_self.temp.remark === undefined) {
                    alert('备注为必需的');
                    return false;
                }
                _self.add_item.helpDate = _self.temp.helpDate;
                _self.add_item.money = _self.temp.money;
                _self.add_item.remark = _self.temp.remark;

                var data = {
                    "helpDate": _self.add_item.helpDate,
                    "deptId": _self.add_item.deptId,
                    "stuId": _self.add_item.stuId,
                    "szlbdm": 10,
                    "shztm": 1,
                    "remark": _self.add_item.remark,
                    "money": parseInt(_self.add_item.money)
                };
                $.ajax({
                    type: "POST",
                    url: _self.url.addBuzhu_without,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    dataType: "json",
                    success: function(result) {
                        if (result.status === 1) {
                            _self.items.status = '未审核';
                            _self.items.push(_self.add_item);
                            $('#myModal').modal('hide');
                        } else {
                            alert(result.message);
                        }
                        console.log(result);
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*添加数据到下方的表中，审核*/
            join_validate() {
                var _self = this;
                _self.add_item.helpDate = _self.temp.helpDate;
                _self.add_item.money = _self.temp.money;

                var data = {
                    "helpDate": _self.add_item.helpDate,
                    "deptId": _self.add_item.deptId,
                    "stuId": _self.add_item.stuId,
                    "szlbdm": 10,
                    "shztm": 1,
                    "remark": "buzhu",
                    "money": parseInt(_self.add_item.money)
                };
                _self.items = [];
                $.ajax({
                    type: "POST",
                    url: _self.url.addBuzhu,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    dataType: "json",
                    success: function(result) {
                        if (result.status === 1) {
                            _self.items.status = '未审核';
                            _self.items.push(_self.add_item);
                            $('#myModal').modal('hide');
                        } else {
                            alert(result.message);
                        }
                        console.log(result);
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*用户提交查询信息，查询学生信息*/
            queryData: function() {
                var _self = this;
                /*提交数据到服务器 query是要查询的数据对象*/
                console.log(_self.query);
                _self.new_item = [];
                $.ajax({
                    type: "POST",
                    url: _self.url.queryZG,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(_self.query),
                    dataType: "json",
                    success: function(result) {
                        console.log(result);
                        $.each(result.result, function(i, val) {
                            val.status = '未审核';
                            _self.new_item.push(val)
                        });
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            setItem: function(item) {
                console.log(item);
                var _self = this;
                _self.add_item = item;
            },
            /*提交数据*/
            submitData: function() {
                var _self = this;
                var id = '';
                var len = _self.items.length;
                $.each(_self.items, function(i, val) {
                    if (i === len - 1) {
                        id = id + val.id;
                    } else {
                        id = id + val.id + ',';
                    }
                });
                var data = {
                    "buzhuList": id
                }
                var newUrl = _self.url.submit + '?' + 'buzhuList=' + id;
                $.ajax({
                    type: "POST",
                    url: newUrl,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(result) {
                        alert(result.message);
                        /*清空缓存*/
                        $.each(_self.items, function(i, val) {
                            _self.shenhe_items.push(val);
                        })

                        _self.items = [];
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*导出表格*/
            export: function() {
                window.open("./download-form.html")
            },
            /*已提交信息*/
            queryTijiaoData: function() {
                var _self = this;
                _self.items = [];
                /*提交数据到服务器 query是要查询的数据对象*/
                var url = _self.url.queryBuZhu + '?' + 'startPage=' + _self.query.startPage + '&pageNum=' + _self.query.pageNum + '&statusCode=' + _self.query.statusCode;
                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(result) {
                        $.each(result.body.result, function(i, val) {
                            _self.items.push(val)
                        });
                        if (result.body.total > 10) {
                            /*如果总数大于10就分页*/
                            /*_self.totalPage 总页数*/
                            _self.totalPage = Math.ceil(result.body.total / _self.query.pageNum);
                            var result = pagin.paginGetli(_self.totalPage, _self.curPage);
                            _self.pages = result.pages;
                        }
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
        }
    });
    buzhu_table.init();
});